package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.BookCustomer;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.utils.Constant;

public class BookCustomerDAO extends BaseDAO<BookCustomer>{

	public void addBookCustomer(BookCustomer customer) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_book_customer(");
		sb.append("userId,name,address,link,description_mc,sellerId,type");
		sb.append(") values (");
		sb.append(":userId,:name,:address,:link,:description_mc,:sellerId,:type");
		sb.append(")");
		
		update(sb.toString(), customer);
	}
	
	public void updateBookCustomer(BookCustomer customer) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("update him_book_customer set ");
		sb.append(" name=:name,address=:address,link=:link,description_mc=:description_mc");
		sb.append(" where userId = :userId");
		
		update(sb.toString(), customer);
	}
	
	public List<BookCustomer> listAllInfos(int type) throws Exception{
		String sql = "select * from him_book_customer where type = ?";
		return getList(sql, BookCustomer.class,type);
	}
	
	public BookCustomer getById(long userId) throws Exception{
		String sql = "select * from him_book_customer where userId = ?";
		return this.getJavaBean(sql, BookCustomer.class, userId);
	}
	
	public void delBookCustomer(long userId) throws Exception{
		String sql = "delete from him_book_customer where userId = ?";
		this.commonUpdate(sql, userId);
	}
	
	public PageModel getPagedInfos(PageModel model,String search,long sellerUserId,long sellerId,int type) throws Exception{
		StringBuilder querySQL = new StringBuilder(" select * from (select a.*,b.username,c.sellerName,c.userId as sellerUserId from him_book_customer a,him_user b,him_book_seller c where a.userId = b.userId and a.sellerId = c.sellerId) m ");
		StringBuilder countSQL = new StringBuilder(" select count(*) from (select a.*,b.username,c.sellerName,c.userId as sellerUserId from him_book_customer a,him_user b,him_book_seller c where a.userId = b.userId and a.sellerId = c.sellerId) m ");
		
		StringBuilder whereSQL = new StringBuilder(" where type = ? and (username like ? or name like ? or address like ? or link like ? ) ");
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(type);
		paramList.add("%"+search+"%");
		paramList.add("%"+search+"%");
		paramList.add("%"+search+"%");
		paramList.add("%"+search+"%");
		
		if(type == Constant.TYPE_BOOK_TICKET){
			querySQL = new StringBuilder(" select * from (select a.*,b.username from him_book_customer a,him_user b where a.userId = b.userId) m ");
			countSQL = new StringBuilder(" select count(*) from (select a.*,b.username from him_book_customer a,him_user b where a.userId = b.userId) m ");
		}else{
			if(sellerUserId > 0){
				whereSQL.append(" and sellerUserId = ? ");
				paramList.add(sellerUserId);
			}
			
			if(sellerId > 0){
				whereSQL.append(" and sellerId = ? ");
				paramList.add(sellerId);
			}
		}
		
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, BookCustomer.class);
	}
}
